package com.kang.gaklibrary.controller;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import static com.fasterxml.jackson.databind.type.LogicalType.Map;

@Controller
public class MyTestController {

    @RequestMapping("/fileLoad")
    public String file(){
        return "a";
    }

    @PostMapping("/fileLoad")
    @ResponseBody
    public String fileLoad(HttpServletRequest request, @RequestParam("myfile")MultipartFile multipartFile,
                           @RequestParam("username") String username) throws Exception {

        File file = new File("E:\\bbbbbb\\testFile.txt");
        // 创建多级文件夹(也会创建文件)
        file.mkdirs();
        // 写入
        multipartFile.transferTo(file);
        return "用户名：" + username;


/*        // 不适用框架的方法：
        // 建立磁盘工厂 缓冲区和磁盘目录
        DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
        // 对接受的数据项解析的类
        ServletFileUpload fileUpload = new ServletFileUpload(diskFileItemFactory);
        // 接受设置
//        fileUpload.setFileSizeMax(1024 * 1024 * 10);// 最大上传10M
        fileUpload.setHeaderEncoding("utf-8");// 设置编码格式
        // 解析数据
        List<FileItem> fileItems = fileUpload.parseRequest(request);
        if (fileItems.size() == 0){
            System.out.println("222222222222222222222222222222222222222");
        }

        Map<String,String> map = new HashMap<>();
        System.out.println("1111111111111111111111111111111111111111111111");
        // 将得到的所有数据项循环处理
        for(FileItem fileItem : fileItems){
            System.out.println("--------------------------------------------");
            // 如果是普通表单成员
            if (fileItem.isFormField()){
                String name = fileItem.getName();
                String value = fileItem.getString();
                map.put(name, value);
                System.out.println("........................................");
            }
            // 是提交的文件项
            else{
                System.out.println("。。。。。。。。。。。。。。。。。。。。。。。。");
                // 得到接收到的文件名称，并生成唯一的文件名
                String name = fileItem.getName();
                String s = UUID.randomUUID().toString();
                String fileName = name + s;

                // 创建接受这个文件的文件对象
                String path = "E:\\abcd\\";
                File file = new File(path + fileName);

                fileItem.write(file);

                // 由输入流和输出流接受文件
                InputStream inputStream = fileItem.getInputStream();
                FileOutputStream fileOutputStream = new FileOutputStream(file);
                byte[] bytes = new byte[1024];
                int len = -1;
                while((len = inputStream.read(bytes)) != -1){
                    fileOutputStream.write(bytes,0,len);
                }
            }
        }*/
    }

}
